package dao;

import java.util.Set;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import domain.Address;
import util.HibernateUtil;

import java.util.*;
import domain.Contact;
import domain.ContactGroup;

public class DAOContact {

	public boolean addContact(
			final Long id,
			final String firstName, 
			final String lastName, 
			final String email, Address add, Set<ContactGroup> books) {

		try{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();
		
		Contact contact = new Contact();
		contact.setEmail(email);
		contact.setFirstName(firstName);
		contact.setLastName(lastName);
		contact.setAdd(add);
		contact.setBooks(books);
		
		session.persist(add);
		session.persist(contact);
		
		tx.commit();
		session.close();
		}catch(Exception e){
			return false;
		}	
		return true;
	}

	public List<Contact> getAll(){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Query query = session.createQuery("from Contact");
		List<Contact> contact = query.list();
		session.close();
		return contact;
	}
	
	
	public String updateContact(
			final Long id,
			final String firstName, 
			final String lastName, 
			final String email,
			final Long add) {

		return null;
	}
	
	public boolean removeContact(
			final long id) {
		
		return true;
	}
	
	public Contact searchContact(
			final Long id) {

		return new Contact();
	}
}
